package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

public class FoodDaoImpl implements FoodDao {

		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		@Override
		public List<Food> listFoodByBusinessId(Integer businessId) {
			List<Food> list = new ArrayList<>();
			String sql = "SELECT * FROM food WHERE businessId=? ";
			
			try {
				con=DBUtil.getConnection();
				pst=con.prepareStatement(sql);
				pst.setInt(1, businessId);
				rs = pst.executeQuery();
				while(rs.next()){
					Food food=new Food();
					food.setBusinessId(rs.getInt("businessId"));
					food.setFoodExplain(rs.getString("foodExplain"));
					food.setFoodId(rs.getInt("foodId"));
					food.setFoodName(rs.getString("foodName"));
					food.setFoodPrice(rs.getDouble("foodPrice"));
					list.add(food);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.CloseAll(rs, pst, con);
			}
			return list;
		}
		@Override
		public int saveFood(Food food) {
			int result = 0;
			String sql = "INSERT INTO food VALUES (null,?,?,?,?) ";
			
			try {
				con=DBUtil.getConnection();
				pst=con.prepareStatement(sql);
				pst.setString(1, food.getFoodName());
				pst.setString(2, food.getFoodExplain());
				pst.setDouble(3, food.getFoodPrice());
				pst.setInt(4, food.getBusinessId());
				result = pst.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.CloseAll(rs, pst, con);
			}
			return result;
		}
		@Override
		public int updateFood(Food food) {
			int result=0;
			String sql="UPDATE food SET foodName=?,foodExplain=?,foodPrice=? WHERE foodId=? ";
			try{
			con = DBUtil.getConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, food.getFoodName());
			pst.setString(2, food.getFoodExplain());
			pst.setDouble(3, food.getFoodPrice());
			pst.setInt(4, food.getFoodId());
			result = pst.executeUpdate();
			
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.CloseAll(rs, pst, con);
			}
			return result;
			
		}
		@Override
		public Food getFoodById(Integer foodId) {
			
			List<Food> list = new ArrayList<>();
			String sql = "SELECT * FROM food WHERE foodId=? ";
			Food food=new Food();
			try {
				con=DBUtil.getConnection();
				pst=con.prepareStatement(sql);
				pst.setInt(1, foodId);
				rs = pst.executeQuery();
				
				while(rs.next()){
					
					food.setBusinessId(rs.getInt("businessId"));
					food.setFoodExplain(rs.getString("foodExplain"));
					food.setFoodId(rs.getInt("foodId"));
					food.setFoodName(rs.getString("foodName"));
					food.setFoodPrice(rs.getDouble("foodPrice"));
					list.add(food);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DBUtil.CloseAll(rs, pst, con);
			}
			return food;
			
		}
		@Override
		public int removeFood(Integer foodId) {
			int result=0;
			String delFoodsql = "DELETE FROM food WHERE foodId=? ";
			try{
				//1.连接数据库
				con = DBUtil.getConnection();
				//2.开启一个事务
				con.setAutoCommit(false);
				
				//删除食品信息
				pst = con.prepareStatement(delFoodsql);
				pst.setInt(1, foodId);
				result = pst.executeUpdate();
				//事务的提交
				con.commit();
				
				} catch (SQLException e) {
					e.printStackTrace();
					result = 0;
					try {
						//事务的回滚
						con.rollback();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}finally{
					DBUtil.CloseAll(rs, pst, con);
				}
			return result;
		}
}
		



